﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SevenZip;
using System.IO;
using System.Runtime.InteropServices;

namespace SeniorCapstoneProject
{
    class Compress
    {
        private string fileName, password, location;
        
        // Set and Get 7zip filename.
        public string FileName
        {
            get
            {
                return fileName;
            }
            set
            {
                fileName = value;
            }
        }

        // Set and Get 7zip encryption password.
        public string Password
        {
            get
            {
                return password;
            }
            set
            {
                password = value;
            }
        }

        // Set and Get 7zip file location.
        public string Location
        {
            get
            {
                return location;
            }
            set
            {
                location = value;
            }
        }

        #region SevenZipCompressor: Set 7zip properties before starting compression.
        public SevenZipCompressor SetProperties(Compress fileInfo)
        {
            SevenZipCompressor compressProp = new SevenZipCompressor();
            compressProp.CompressionMode = CompressionMode.Create; //Define compression mode
            compressProp.DirectoryStructure = false; //Set DirectoryStructure
            compressProp.EncryptHeaders = true; //EncryptHeaders
            compressProp.ZipEncryptionMethod = ZipEncryptionMethod.Aes256; //Encryption on files needed
            compressProp.CompressionMethod = CompressionMethod.Default; //Define compression method
            compressProp.CompressionLevel = CompressionLevel.Fast; //Set the amount of passes to make
            compressProp.DefaultItemName = FileName; //Set item name to be stored in compressed 7z

            return compressProp;
        }
        #endregion

        #region StartCompression: Start compression of the files to be stored then transfered to the server
        //[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public void StartCompression(SevenZipCompressor zipProperties, Compress fileInfo, MemoryStream inStream)
        {
            using (inStream) //Use memorystream sent from FileSpliterJoiner.cs
            {
                using (FileStream outStream = File.Create(fileInfo.Location + ".7z")) //Create stream to store file.
                {
                    zipProperties.CompressStream(inStream, outStream, fileInfo.Password); //Start compression
                    outStream.Flush(); //Save changes to file.
                    outStream.Close(); //Close file.
                }
                inStream.Flush(); //Save changes to memorystream...Not needed
                inStream.Close(); //Close memorystream and free up resources for next file
            }
        }
        #endregion

        #region StartDecompression: Extract files from specified location after all transfer of files has completed
        public void StartDecompression(Compress fileInfo, String outLocation)
        {
            using (FileStream inStream = File.OpenRead(fileInfo.Location)) //Open file to be sent to extractor
            {
                SevenZipExtractor zipExtractor = new SevenZipExtractor(inStream, fileInfo.Password); //Pass stream and password for extraction
                zipExtractor.ExtractArchive(outLocation); //Extract file to given location
            }
        }
        #endregion
    }
}
